<template>
  <div class="qualification-review">
    <!-- 面包屑导航 -->
    <a-breadcrumb class="breadcrumb">
      <a-breadcrumb-item>
        <router-link to="/salvation">超度与救赎</router-link>
      </a-breadcrumb-item>
      <a-breadcrumb-item>
        <router-link to="/salvation/transcendence-application">超度申请管理</router-link>
      </a-breadcrumb-item>
      <a-breadcrumb-item>资格审核</a-breadcrumb-item>
    </a-breadcrumb>

    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="page-title">资格审核</div>
        <div class="page-subtitle">审核超度申请者和亡魂的资格条件</div>
      </div>
      <div class="header-actions">
        <a-button type="primary" @click="handleBatchReview" :disabled="selectedRowKeys.length === 0">
          <AuditOutlined />
          批量审核 ({{ selectedRowKeys.length }})
        </a-button>
      </div>
    </div>

    <!-- 审核统计 -->
    <div class="review-stats">
      <a-row :gutter="[20, 20]">
        <a-col :xs="12" :sm="6" :lg="6">
          <div class="stat-card">
            <div class="stat-icon">
              <ClockCircleOutlined />
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ reviewStats.pending }}</div>
              <div class="stat-label">待审核</div>
            </div>
          </div>
        </a-col>
        <a-col :xs="12" :sm="6" :lg="6">
          <div class="stat-card">
            <div class="stat-icon">
              <CheckCircleOutlined />
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ reviewStats.qualified }}</div>
              <div class="stat-label">审核通过</div>
            </div>
          </div>
        </a-col>
        <a-col :xs="12" :sm="6" :lg="6">
          <div class="stat-card">
            <div class="stat-icon">
              <CloseCircleOutlined />
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ reviewStats.unqualified }}</div>
              <div class="stat-label">审核未通过</div>
            </div>
          </div>
        </a-col>
        <a-col :xs="12" :sm="6" :lg="6">
          <div class="stat-card">
            <div class="stat-icon">
              <TeamOutlined />
            </div>
            <div class="stat-content">
              <div class="stat-value">{{ reviewStats.totalReviewed }}</div>
              <div class="stat-label">今日已审核</div>
            </div>
          </div>
        </a-col>
      </a-row>
    </div>

    <!-- 审核列表 -->
    <div class="review-list">
      <div class="list-header">
        <h3>资格审核管理</h3>
        <div class="header-actions">
          <a-input-search placeholder="搜索申请人或亡魂姓名" style="width: 300px" @search="handleSearch" />
          <a-select v-model:value="statusFilter" placeholder="审核状态" style="width: 120px; margin-left: 12px" @change="handleFilterChange">
            <a-select-option value="">全部状态</a-select-option>
            <a-select-option value="待审核">待审核</a-select-option>
            <a-select-option value="已通过">已通过</a-select-option>
            <a-select-option value="未通过">未通过</a-select-option>
          </a-select>
        </div>
      </div>

      <a-table :columns="columns" :data-source="filteredReviews" :pagination="pagination" :row-selection="rowSelection" row-key="id">
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'status'">
            <a-tag :color="getStatusColor(record.status)">
              {{ record.status }}
            </a-tag>
          </template>
          <template v-if="column.key === 'qualification'">
            <a-rate :value="record.qualificationScore / 20" disabled style="font-size: 14px" />
            <span style="margin-left: 8px; color: rgba(255, 255, 255, 0.8)"> {{ record.qualificationScore }}/100 </span>
          </template>
          <template v-if="column.key === 'urgency'">
            <a-tag :color="getUrgencyColor(record.urgency)">
              {{ record.urgency }}
            </a-tag>
          </template>
          <template v-if="column.key === 'actions'">
            <a-space>
              <a-button type="link" size="small" @click="reviewDetail(record)"> 审核详情 </a-button>
              <a-button type="link" size="small" @click="processReview(record)" :disabled="record.status !== '待审核'"> 立即审核 </a-button>
            </a-space>
          </template>
        </template>
      </a-table>
    </div>

    <!-- 审核详情模态框 -->
    <a-modal v-model:open="showDetailModal" title="资格审核详情" width="1000px" :footer="null">
      <div class="review-detail" v-if="selectedReview">
        <a-row :gutter="24">
          <a-col :span="8">
            <a-card class="detail-card">
              <h4>申请基本信息</h4>
              <a-descriptions :column="1" size="small">
                <a-descriptions-item label="审核编号">{{ selectedReview.id }}</a-descriptions-item>
                <a-descriptions-item label="亡魂姓名">{{ selectedReview.deceasedName }}</a-descriptions-item>
                <a-descriptions-item label="申请人">{{ selectedReview.applicantName }}</a-descriptions-item>
                <a-descriptions-item label="关系">{{ selectedReview.relationship }}</a-descriptions-item>
                <a-descriptions-item label="申请类型">
                  <a-tag :color="getTypeColor(selectedReview.transcendenceType)">
                    {{ selectedReview.transcendenceType }}
                  </a-tag>
                </a-descriptions-item>
                <a-descriptions-item label="申请时间">{{ selectedReview.applicationTime }}</a-descriptions-item>
                <a-descriptions-item label="当前状态">
                  <a-tag :color="getStatusColor(selectedReview.status)">
                    {{ selectedReview.status }}
                  </a-tag>
                </a-descriptions-item>
              </a-descriptions>
            </a-card>
          </a-col>

          <a-col :span="16">
            <a-card class="detail-card">
              <h4>资格评估</h4>
              <a-row :gutter="16">
                <a-col :span="12">
                  <div class="score-section">
                    <h5>综合评分</h5>
                    <a-progress type="circle" :percent="selectedReview.qualificationScore" :stroke-color="getScoreColor(selectedReview.qualificationScore)" :size="80" />
                    <p>{{ selectedReview.qualificationScore }}/100 分</p>
                  </div>
                </a-col>
                <a-col :span="12">
                  <div class="criteria-section">
                    <h5>评估维度</h5>
                    <div class="criteria-item">
                      <span class="criteria-label">申请人资格:</span>
                      <a-rate :value="selectedReview.applicantQualification / 20" disabled style="font-size: 12px" />
                      <span>{{ selectedReview.applicantQualification }}/100</span>
                    </div>
                    <div class="criteria-item">
                      <span class="criteria-label">亡魂条件:</span>
                      <a-rate :value="selectedReview.deceasedQualification / 20" disabled style="font-size: 12px" />
                      <span>{{ selectedReview.deceasedQualification }}/100</span>
                    </div>
                    <div class="criteria-item">
                      <span class="criteria-label">紧急程度:</span>
                      <a-rate :value="selectedReview.urgencyScore / 20" disabled style="font-size: 12px" />
                      <span>{{ selectedReview.urgencyScore }}/100</span>
                    </div>
                    <div class="criteria-item">
                      <span class="criteria-label">资料完整:</span>
                      <a-rate :value="selectedReview.documentScore / 20" disabled style="font-size: 12px" />
                      <span>{{ selectedReview.documentScore }}/100</span>
                    </div>
                  </div>
                </a-col>
              </a-row>
            </a-card>
          </a-col>
        </a-row>

        <a-card class="detail-card" style="margin-top: 16px">
          <h4>申请详情</h4>
          <a-descriptions :column="2" size="small">
            <a-descriptions-item label="死亡时间">{{ selectedReview.deathDate }}</a-descriptions-item>
            <a-descriptions-item label="联系电话">{{ selectedReview.phone }}</a-descriptions-item>
            <a-descriptions-item label="紧急程度">
              <a-tag :color="getUrgencyColor(selectedReview.urgency)">
                {{ selectedReview.urgency }}
              </a-tag>
            </a-descriptions-item>
            <a-descriptions-item label="提交时间">{{ selectedReview.applicationTime }}</a-descriptions-item>
            <a-descriptions-item label="申请原因" :span="2">{{ selectedReview.reason }}</a-descriptions-item>
          </a-descriptions>
        </a-card>

        <a-card class="detail-card" style="margin-top: 16px" v-if="selectedReview.reviewHistory">
          <h4>审核历史</h4>
          <a-timeline>
            <a-timeline-item v-for="item in selectedReview.reviewHistory" :key="item.id" :color="getTimelineColor(item.type)">
              <template #dot>
                <component :is="getTimelineIcon(item.type)" />
              </template>
              <div class="timeline-content">
                <div class="timeline-title">{{ item.title }}</div>
                <div class="timeline-description">{{ item.description }}</div>
                <div class="timeline-time">{{ item.time }} - {{ item.reviewer }}</div>
              </div>
            </a-timeline-item>
          </a-timeline>
        </a-card>
      </div>
    </a-modal>

    <!-- 立即审核模态框 -->
    <a-modal v-model:open="showReviewModal" title="资格审核" width="700px" @ok="submitReview" :confirmLoading="saving">
      <a-form :model="reviewForm" layout="vertical" v-if="selectedReview">
        <a-form-item label="申请信息">
          <a-input :value="`${selectedReview.deceasedName} - ${selectedReview.applicantName}`" disabled />
        </a-form-item>

        <a-form-item label="审核决定" required>
          <a-radio-group v-model:value="reviewForm.decision">
            <a-radio value="approve">通过审核</a-radio>
            <a-radio value="reject">拒绝申请</a-radio>
            <a-radio value="pending">需要补充材料</a-radio>
          </a-radio-group>
        </a-form-item>

        <a-row :gutter="16">
          <a-col :span="12">
            <a-form-item label="申请人资格评分" required>
              <a-slider v-model:value="reviewForm.applicantScore" :min="0" :max="100" />
              <div style="text-align: center; margin-top: 8px">{{ reviewForm.applicantScore }}/100</div>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="亡魂条件评分" required>
              <a-slider v-model:value="reviewForm.deceasedScore" :min="0" :max="100" />
              <div style="text-align: center; margin-top: 8px">{{ reviewForm.deceasedScore }}/100</div>
            </a-form-item>
          </a-col>
        </a-row>

        <a-row :gutter="16">
          <a-col :span="12">
            <a-form-item label="紧急程度评分" required>
              <a-slider v-model:value="reviewForm.urgencyScore" :min="0" :max="100" />
              <div style="text-align: center; margin-top: 8px">{{ reviewForm.urgencyScore }}/100</div>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="资料完整度评分" required>
              <a-slider v-model:value="reviewForm.documentScore" :min="0" :max="100" />
              <div style="text-align: center; margin-top: 8px">{{ reviewForm.documentScore }}/100</div>
            </a-form-item>
          </a-col>
        </a-row>

        <a-form-item label="审核意见" required>
          <a-textarea v-model:value="reviewForm.comments" :rows="4" placeholder="请输入详细的审核意见和建议" />
        </a-form-item>

        <a-form-item label="后续建议" v-if="reviewForm.decision === 'reject'">
          <a-textarea v-model:value="reviewForm.suggestions" :rows="3" placeholder="请提供改善建议或重新申请的指导" />
        </a-form-item>

        <a-form-item label="补充材料要求" v-if="reviewForm.decision === 'pending'">
          <a-checkbox-group v-model:value="reviewForm.requiredDocuments">
            <a-checkbox value="identity">身份证明</a-checkbox>
            <a-checkbox value="death_certificate">死亡证明</a-checkbox>
            <a-checkbox value="relationship">关系证明</a-checkbox>
            <a-checkbox value="medical">医疗记录</a-checkbox>
            <a-checkbox value="other">其他材料</a-checkbox>
          </a-checkbox-group>
        </a-form-item>
      </a-form>
    </a-modal>

    <!-- 批量审核模态框 -->
    <a-modal v-model:open="showBatchModal" title="批量审核" width="600px" @ok="submitBatchReview" :confirmLoading="saving">
      <div class="batch-review">
        <a-alert message="批量审核提醒" :description="`您已选择 ${selectedRowKeys.length} 个申请进行批量审核`" type="info" show-icon style="margin-bottom: 16px" />

        <a-form :model="batchForm" layout="vertical">
          <a-form-item label="批量操作" required>
            <a-radio-group v-model:value="batchForm.action">
              <a-radio value="approve">批量通过</a-radio>
              <a-radio value="reject">批量拒绝</a-radio>
            </a-radio-group>
          </a-form-item>

          <a-form-item label="统一评分" v-if="batchForm.action === 'approve'">
            <a-row :gutter="16">
              <a-col :span="12">
                <div>申请人资格: {{ batchForm.scores.applicant }}/100</div>
                <a-slider v-model:value="batchForm.scores.applicant" :min="60" :max="100" />
              </a-col>
              <a-col :span="12">
                <div>亡魂条件: {{ batchForm.scores.deceased }}/100</div>
                <a-slider v-model:value="batchForm.scores.deceased" :min="60" :max="100" />
              </a-col>
            </a-row>
          </a-form-item>

          <a-form-item label="批量意见" required>
            <a-textarea v-model:value="batchForm.comments" :rows="3" placeholder="请输入批量审核的统一意见" />
          </a-form-item>
        </a-form>

        <a-table :columns="batchColumns" :data-source="selectedReviews" :pagination="false" size="small" row-key="id" />
      </div>
    </a-modal>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, h } from 'vue'
import { useSalvationStore } from '@/stores/salvation'
import { AuditOutlined, ClockCircleOutlined, CheckCircleOutlined, CloseCircleOutlined, TeamOutlined, UserOutlined, FileTextOutlined, SettingOutlined } from '@ant-design/icons-vue'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'

const salvationStore = useSalvationStore()

// 模态框状态
const showDetailModal = ref(false)
const showReviewModal = ref(false)
const showBatchModal = ref(false)
const saving = ref(false)
const selectedReview = ref(null)

// 表单数据
const reviewForm = ref({
  decision: '',
  applicantScore: 70,
  deceasedScore: 70,
  urgencyScore: 50,
  documentScore: 80,
  comments: '',
  suggestions: '',
  requiredDocuments: []
})

const batchForm = ref({
  action: '',
  scores: {
    applicant: 75,
    deceased: 75
  },
  comments: ''
})

// 表格行选择
const selectedRowKeys = ref([])
const rowSelection = {
  selectedRowKeys: selectedRowKeys,
  onChange: (keys) => {
    selectedRowKeys.value = keys
  },
  getCheckboxProps: (record) => ({
    disabled: record.status !== '待审核'
  })
}

// 审核统计数据
const reviewStats = ref({
  pending: 23,
  qualified: 89,
  unqualified: 12,
  totalReviewed: 45
})

// 筛选条件
const statusFilter = ref('')
const searchKeyword = ref('')

// 分页配置
const pagination = ref({
  current: 1,
  pageSize: 10,
  total: 0,
  showSizeChanger: true,
  showQuickJumper: true,
  showTotal: (total, range) => `共 ${total} 条记录，第 ${range[0]}-${range[1]} 条`
})

// 模拟审核数据
const reviewData = ref([
  {
    id: 'QR001',
    deceasedName: '王老太',
    applicantName: '王小明',
    relationship: '子女',
    transcendenceType: '普通超度',
    applicationTime: '2024-01-15 10:30:00',
    deathDate: '2024-01-10',
    phone: '138****5678',
    reason: '老人生前心存执念，希望通过超度仪式帮助其超脱轮回，获得安息。',
    status: '待审核',
    qualificationScore: 0,
    applicantQualification: 80,
    deceasedQualification: 75,
    urgencyScore: 60,
    documentScore: 85,
    urgency: '一般',
    reviewNotes: '申请材料齐全，等待详细审核',
    reviewHistory: [
      {
        id: 1,
        type: 'submit',
        title: '提交审核',
        description: '申请已提交，等待资格审核',
        time: '2024-01-15 10:30:00',
        reviewer: '系统'
      }
    ]
  },
  {
    id: 'QR002',
    deceasedName: '李大爷',
    applicantName: '李华',
    relationship: '子女',
    transcendenceType: '紧急超度',
    applicationTime: '2024-01-15 09:15:00',
    deathDate: '2024-01-14',
    phone: '139****2345',
    reason: '父亲因意外离世，家属希望尽快安排超度仪式。',
    status: '已通过',
    qualificationScore: 88,
    applicantQualification: 90,
    deceasedQualification: 85,
    urgencyScore: 95,
    documentScore: 82,
    urgency: '紧急',
    reviewNotes: '符合超度条件，情况紧急，批准通过',
    reviewHistory: [
      {
        id: 1,
        type: 'submit',
        title: '提交审核',
        description: '申请已提交，等待资格审核',
        time: '2024-01-15 09:15:00',
        reviewer: '系统'
      },
      {
        id: 2,
        type: 'approve',
        title: '审核通过',
        description: '经审核，申请人资格符合要求，亡魂条件满足超度标准，批准申请',
        time: '2024-01-15 11:30:00',
        reviewer: '高级审核员A'
      }
    ]
  },
  {
    id: 'QR003',
    deceasedName: '张奶奶',
    applicantName: '张小红',
    relationship: '孙女',
    transcendenceType: '特殊超度',
    applicationTime: '2024-01-13 14:15:00',
    deathDate: '2024-01-12',
    phone: '137****8901',
    reason: '奶奶生前信佛，希望按照佛教仪式超度。',
    status: '未通过',
    qualificationScore: 45,
    applicantQualification: 50,
    deceasedQualification: 40,
    urgencyScore: 30,
    documentScore: 60,
    urgency: '一般',
    reviewNotes: '申请人与亡魂关系证明不足，亡魂生前业障较重，建议补充相关材料',
    reviewHistory: [
      {
        id: 1,
        type: 'submit',
        title: '提交审核',
        description: '申请已提交，等待资格审核',
        time: '2024-01-13 14:15:00',
        reviewer: '系统'
      },
      {
        id: 2,
        type: 'reject',
        title: '审核未通过',
        description: '经评估，申请条件不足，需要补充相关证明材料',
        time: '2024-01-14 10:00:00',
        reviewer: '审核员B'
      }
    ]
  },
  {
    id: 'QR004',
    deceasedName: '陈老师',
    applicantName: '陈小军',
    relationship: '子女',
    transcendenceType: '普通超度',
    applicationTime: '2024-01-12 11:45:00',
    deathDate: '2024-01-11',
    phone: '136****7890',
    reason: '父亲是一名退休教师，希望通过超度帮助其灵魂得到安息。',
    status: '待审核',
    qualificationScore: 0,
    applicantQualification: 85,
    deceasedQualification: 90,
    urgencyScore: 70,
    documentScore: 95,
    urgency: '一般',
    reviewNotes: '申请人资格良好，材料完整，待进一步评估',
    reviewHistory: [
      {
        id: 1,
        type: 'submit',
        title: '提交审核',
        description: '申请已提交，等待资格审核',
        time: '2024-01-12 11:45:00',
        reviewer: '系统'
      }
    ]
  }
])

// 表格列配置
const columns = [
  {
    title: '审核编号',
    dataIndex: 'id',
    key: 'id',
    width: 100
  },
  {
    title: '亡魂姓名',
    dataIndex: 'deceasedName',
    key: 'deceasedName',
    width: 120
  },
  {
    title: '申请人',
    dataIndex: 'applicantName',
    key: 'applicantName',
    width: 120
  },
  {
    title: '申请类型',
    dataIndex: 'transcendenceType',
    key: 'transcendenceType',
    width: 120
  },
  {
    title: '提交时间',
    dataIndex: 'applicationTime',
    key: 'applicationTime',
    width: 160
  },
  {
    title: '审核状态',
    dataIndex: 'status',
    key: 'status',
    width: 100
  },
  {
    title: '资格评分',
    dataIndex: 'qualification',
    key: 'qualification',
    width: 150
  },
  {
    title: '紧急程度',
    dataIndex: 'urgency',
    key: 'urgency',
    width: 100
  },
  {
    title: '操作',
    key: 'actions',
    width: 160
  }
]

// 批量审核表格列配置
const batchColumns = [
  {
    title: '亡魂姓名',
    dataIndex: 'deceasedName',
    key: 'deceasedName',
    width: 120
  },
  {
    title: '申请人',
    dataIndex: 'applicantName',
    key: 'applicantName',
    width: 120
  },
  {
    title: '申请类型',
    dataIndex: 'transcendenceType',
    key: 'transcendenceType',
    width: 120
  },
  {
    title: '当前状态',
    dataIndex: 'status',
    key: 'status',
    width: 100,
    customRender: ({ record }) => {
      return h('a-tag', { color: getStatusColor(record.status) }, record.status)
    }
  }
]

// 过滤后的审核列表
const filteredReviews = computed(() => {
  let result = reviewData.value

  // 状态筛选
  if (statusFilter.value) {
    result = result.filter((review) => review.status === statusFilter.value)
  }

  // 关键词搜索
  if (searchKeyword.value) {
    result = result.filter((review) => review.deceasedName.includes(searchKeyword.value) || review.applicantName.includes(searchKeyword.value))
  }

  pagination.value.total = result.length
  return result
})

// 选中的审核项
const selectedReviews = computed(() => {
  return reviewData.value.filter((item) => selectedRowKeys.value.includes(item.id))
})

// 重置表单
const resetReviewForm = () => {
  reviewForm.value = {
    decision: '',
    applicantScore: 70,
    deceasedScore: 70,
    urgencyScore: 50,
    documentScore: 80,
    comments: '',
    suggestions: '',
    requiredDocuments: []
  }
}

const resetBatchForm = () => {
  batchForm.value = {
    action: '',
    scores: {
      applicant: 75,
      deceased: 75
    },
    comments: ''
  }
}

// 搜索处理
const handleSearch = (value) => {
  searchKeyword.value = value
}

// 筛选变更处理
const handleFilterChange = () => {
  pagination.value.current = 1
}

// 审核详情
const reviewDetail = (record) => {
  selectedReview.value = {
    ...record,
    qualificationScore: record.qualificationScore || Math.round((record.applicantQualification + record.deceasedQualification + record.urgencyScore + record.documentScore) / 4)
  }
  showDetailModal.value = true
}

// 处理审核
const processReview = (record) => {
  selectedReview.value = record
  resetReviewForm()

  // 预设分数
  reviewForm.value.applicantScore = record.applicantQualification
  reviewForm.value.deceasedScore = record.deceasedQualification
  reviewForm.value.urgencyScore = record.urgencyScore
  reviewForm.value.documentScore = record.documentScore

  showReviewModal.value = true
}

// 批量审核
const handleBatchReview = () => {
  if (selectedRowKeys.value.length === 0) {
    message.warning('请选择要批量审核的申请')
    return
  }

  resetBatchForm()
  showBatchModal.value = true
}

// 提交审核
const submitReview = async () => {
  if (!reviewForm.value.decision || !reviewForm.value.comments) {
    message.error('请填写审核决定和审核意见')
    return
  }

  saving.value = true
  try {
    await new Promise((resolve) => setTimeout(resolve, 1000))

    const index = reviewData.value.findIndex((item) => item.id === selectedReview.value.id)
    if (index !== -1) {
      const totalScore = Math.round((reviewForm.value.applicantScore + reviewForm.value.deceasedScore + reviewForm.value.urgencyScore + reviewForm.value.documentScore) / 4)

      const statusMap = {
        approve: '已通过',
        reject: '未通过',
        pending: '待补充'
      }

      reviewData.value[index] = {
        ...reviewData.value[index],
        status: statusMap[reviewForm.value.decision],
        qualificationScore: totalScore,
        applicantQualification: reviewForm.value.applicantScore,
        deceasedQualification: reviewForm.value.deceasedScore,
        urgencyScore: reviewForm.value.urgencyScore,
        documentScore: reviewForm.value.documentScore,
        reviewNotes: reviewForm.value.comments
      }

      // 添加审核历史
      reviewData.value[index].reviewHistory.push({
        id: reviewData.value[index].reviewHistory.length + 1,
        type: reviewForm.value.decision,
        title: getActionTitle(reviewForm.value.decision),
        description: reviewForm.value.comments,
        time: dayjs().format('YYYY-MM-DD HH:mm:ss'),
        reviewer: '当前用户'
      })

      // 更新统计
      if (reviewForm.value.decision === 'approve') {
        reviewStats.value.qualified += 1
        reviewStats.value.pending -= 1
      } else if (reviewForm.value.decision === 'reject') {
        reviewStats.value.unqualified += 1
        reviewStats.value.pending -= 1
      }
      reviewStats.value.totalReviewed += 1
    }

    message.success('审核完成')
    showReviewModal.value = false
    resetReviewForm()
  } catch (error) {
    message.error('审核失败，请重试')
  } finally {
    saving.value = false
  }
}

// 提交批量审核
const submitBatchReview = async () => {
  if (!batchForm.value.action || !batchForm.value.comments) {
    message.error('请填写批量操作和审核意见')
    return
  }

  saving.value = true
  try {
    await new Promise((resolve) => setTimeout(resolve, 1500))

    const statusMap = {
      approve: '已通过',
      reject: '未通过'
    }

    selectedRowKeys.value.forEach((id) => {
      const index = reviewData.value.findIndex((item) => item.id === id)
      if (index !== -1) {
        const scores =
          batchForm.value.action === 'approve'
            ? {
                applicantQualification: batchForm.value.scores.applicant,
                deceasedQualification: batchForm.value.scores.deceased,
                urgencyScore: 70,
                documentScore: 80
              }
            : {
                applicantQualification: 40,
                deceasedQualification: 30,
                urgencyScore: 20,
                documentScore: 50
              }

        const totalScore = Math.round((scores.applicantQualification + scores.deceasedQualification + scores.urgencyScore + scores.documentScore) / 4)

        reviewData.value[index] = {
          ...reviewData.value[index],
          status: statusMap[batchForm.value.action],
          qualificationScore: totalScore,
          ...scores,
          reviewNotes: batchForm.value.comments
        }

        // 添加审核历史
        reviewData.value[index].reviewHistory.push({
          id: reviewData.value[index].reviewHistory.length + 1,
          type: batchForm.value.action,
          title: `批量${getActionTitle(batchForm.value.action)}`,
          description: batchForm.value.comments,
          time: dayjs().format('YYYY-MM-DD HH:mm:ss'),
          reviewer: '当前用户'
        })
      }
    })

    // 更新统计
    if (batchForm.value.action === 'approve') {
      reviewStats.value.qualified += selectedRowKeys.value.length
      reviewStats.value.pending -= selectedRowKeys.value.length
    } else {
      reviewStats.value.unqualified += selectedRowKeys.value.length
      reviewStats.value.pending -= selectedRowKeys.value.length
    }
    reviewStats.value.totalReviewed += selectedRowKeys.value.length

    message.success(`批量审核完成，处理了 ${selectedRowKeys.value.length} 个申请`)
    selectedRowKeys.value = []
    showBatchModal.value = false
    resetBatchForm()
  } catch (error) {
    message.error('批量审核失败，请重试')
  } finally {
    saving.value = false
  }
}

// 获取操作标题
const getActionTitle = (action) => {
  const titles = {
    approve: '审核通过',
    reject: '审核拒绝',
    pending: '待补充材料'
  }
  return titles[action] || '处理完成'
}

// 获取状态颜色
const getStatusColor = (status) => {
  const colors = {
    待审核: 'orange',
    待补充: 'purple',
    已通过: 'green',
    未通过: 'red'
  }
  return colors[status] || 'default'
}

// 获取类型颜色
const getTypeColor = (type) => {
  const colors = {
    普通超度: 'blue',
    紧急超度: 'red',
    特殊超度: 'purple',
    集体超度: 'green'
  }
  return colors[type] || 'default'
}

// 获取紧急程度颜色
const getUrgencyColor = (urgency) => {
  const colors = {
    特急: 'red',
    紧急: 'orange',
    一般: 'blue',
    低: 'green'
  }
  return colors[urgency] || 'default'
}

// 获取评分颜色
const getScoreColor = (score) => {
  if (score >= 80) return '#52c41a'
  if (score >= 60) return '#1890ff'
  if (score >= 40) return '#faad14'
  return '#f5222d'
}

// 获取时间轴颜色
const getTimelineColor = (type) => {
  const colors = {
    submit: 'blue',
    approve: 'green',
    reject: 'red',
    pending: 'orange'
  }
  return colors[type] || 'blue'
}

// 获取时间轴图标
const getTimelineIcon = (type) => {
  const icons = {
    submit: UserOutlined,
    approve: CheckCircleOutlined,
    reject: CloseCircleOutlined,
    pending: FileTextOutlined
  }
  return icons[type] || UserOutlined
}

onMounted(() => {
  console.log('资格审核页面已加载')
})
</script>

<style lang="scss" scoped>
.qualification-review {
  padding: 24px;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
  min-height: 100vh;
  color: #fff;

  // 面包屑导航
  .breadcrumb {
    margin-bottom: 24px;

    :deep(.ant-breadcrumb-link) {
      color: rgba(255, 255, 255, 0.8);

      &:hover {
        color: #ffd700;
      }
    }

    :deep(.ant-breadcrumb-separator) {
      color: rgba(255, 255, 255, 0.6);
    }
  }

  // 页面头部
  .page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 32px;
    padding-bottom: 20px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);

    .header-content {
      .page-title {
        font-size: 2rem;
        font-weight: bold;
        color: #fff;
        margin-bottom: 8px;
      }

      .page-subtitle {
        color: rgba(255, 255, 255, 0.8);
        font-size: 1rem;
      }
    }
  }

  // 审核统计
  .review-stats {
    margin-bottom: 32px;

    .stat-card {
      background: rgba(255, 255, 255, 0.1);
      border: 1px solid rgba(255, 255, 255, 0.2);
      border-radius: 12px;
      padding: 20px;
      display: flex;
      align-items: center;
      gap: 16px;
      transition: all 0.3s ease;

      .stat-icon {
        width: 50px;
        height: 50px;
        border-radius: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.5rem;
        background: rgba(255, 215, 0, 0.2);
        color: #ffd700;
      }

      .stat-content {
        .stat-value {
          font-size: 1.8rem;
          font-weight: bold;
          color: #fff;
          margin-bottom: 4px;
        }

        .stat-label {
          color: rgba(255, 255, 255, 0.8);
          font-size: 0.9rem;
        }
      }
    }
  }

  // 审核列表
  .review-list {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 12px;
    padding: 24px;

    .list-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 24px;
      padding-bottom: 12px;
      border-bottom: 1px solid rgba(255, 255, 255, 0.1);

      h3 {
        margin: 0;
        color: #fff;
        font-size: 1.2rem;
      }

      .header-actions {
        display: flex;
        align-items: center;
      }
    }

    :deep(.ant-table) {
      background: transparent;

      .ant-table-thead > tr > th {
        background: rgba(255, 255, 255, 0.1);
        color: rgba(255, 255, 255, 0.9);
        border-bottom: 1px solid rgba(255, 255, 255, 0.2);
      }

      .ant-table-tbody > tr > td {
        background: transparent;
        color: rgba(255, 255, 255, 0.8);
        border-bottom: 1px solid rgba(255, 255, 255, 0.1);
      }

      .ant-table-tbody > tr:hover > td {
        background: rgba(255, 255, 255, 0.05);
      }
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .qualification-review {
    padding: 16px;

    .page-header {
      flex-direction: column;
      align-items: flex-start;
      gap: 16px;

      .header-content .page-title {
        font-size: 1.5rem;
      }
    }

    .stat-card {
      flex-direction: column;
      text-align: center;
      gap: 12px;

      .stat-content .stat-value {
        font-size: 1.5rem;
      }
    }

    .review-list .list-header {
      flex-direction: column;
      align-items: flex-start;
      gap: 12px;
    }
  }
}
</style>
